package com.example.aacwebdemo.es2;


import com.google.gson.Gson;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedLongTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author zhao.xin06679
 * @date 2021年06月22日 18:26
 */
@RestController
@RequestMapping("/es2")
public class EsController2 {

    @Autowired
    RestHighLevelClient client;

    @RequestMapping(value="saveTest",method = RequestMethod.GET)
    public String saveTest() throws IOException {

        IndexRequest request = new IndexRequest("mahaah"); //索引
        request.id("1"); //文档id
        EsBeanUserAction zz= EsBeanUserAction.build(1);
        Gson g = new Gson();
        String s = g.toJson(zz);
        System.out.println(s);
        request.source(s, XContentType.JSON);

        IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);

        return "success";
    }

    @RequestMapping(value="findTest",method = RequestMethod.GET)
    public String findTest() throws IOException {
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        //根据姓名进行分组统计个数
        TermsAggregationBuilder field = AggregationBuilders.terms("group_by_sectorID").field("sectorID").size(1000000);
        //ValueCountAggregationBuilder countField = AggregationBuilders.count("count_sectorID").field("sectorID");
        //field.subAggregation(countField);
        searchSourceBuilder.aggregation(field);
        SearchRequest searchRequest = new SearchRequest("test_user_action_2").source(searchSourceBuilder);

        System.out.println(searchRequest.toString());

        SearchResponse response = client.search(searchRequest,RequestOptions.DEFAULT);
        //分组在es中是分桶
        ParsedLongTerms termsName = response.getAggregations().get("group_by_sectorID");
        List<? extends Terms.Bucket> buckets = termsName.getBuckets();
        buckets.forEach(naem -> {
            Long key = (Long) naem.getKey();
            Long docCount = naem.getDocCount();

            System.out.println(key + ":"+ docCount);
        });


        return null;
    }

    public static void main(String[] args) {
//        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//        //根据姓名进行分组统计个数
//        TermsAggregationBuilder field = AggregationBuilders.terms("group_by_sectorID").field("sectorID").size(1000000);
//        //ValueCountAggregationBuilder countField = AggregationBuilders.count("count_sectorID").field("sectorID");
//        //field.subAggregation(countField);
//        BoolQueryBuilder typeQueryBuilder = new BoolQueryBuilder();
//        byte a = (byte) 0x10100001;
//        byte b = (byte) 0x10100010;
//
//        typeQueryBuilder.filter(QueryBuilders.termsQuery("sectorID",new Byte[]{a,b}));
//
//        searchSourceBuilder.query(typeQueryBuilder);
//
//        searchSourceBuilder.aggregation(field);
//        SearchRequest searchRequest = new SearchRequest("test_user_action_2").source(searchSourceBuilder);
//        System.out.println(searchRequest.toString());



    }

}
